package com.lsh.streamgd.controller;

import com.lsh.streamgd.beans.TaskConfig;
import com.lsh.streamgd.dao.TaskRecord;
import com.lsh.streamgd.utils.Constants;
import com.lsh.streamgd.utils.Settings;
import com.lsh.streamgd.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ljgsonx on 2017/1/12.
 */

@Controller
@RequestMapping("/task")
public class TaskController {

    private static Logger logger = LoggerFactory.getLogger("TaskController");

    private String getConfigFilePath() {
        return Settings.webappRoot + "taskConfig\\config.rdf";
    }

    /**
     * 创建task
     *
     * @param taskConfig
     * @return
     */
    @RequestMapping(value = "/create", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String create(TaskConfig taskConfig) {
        logger.info("\n=====[  create task request ]=====\n params:{}", taskConfig);
        try {
            if (!checkParams(taskConfig)) {
                return Constants.AjaxCode.PARAMLOSS;
            }
            TaskRecord.addRecord(taskConfig, getConfigFilePath());
        } catch (Exception e) {
            return Constants.AjaxCode.ERROR_UNKOWN;
        }
        return Constants.AjaxCode.SUCCESS;
    }

    /**
     * 获取未删除的task列表
     *
     * @return
     */
    @RequestMapping(value = "/list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String taskList() {
        List<TaskConfig> list = TaskRecord.getAllTaskConfigs(getConfigFilePath());
        return StringUtil.getJSONDefault(list);
    }

    /**
     * 软删除task
     *
     * @param taskId
     * @return
     */
    @RequestMapping(value = "/del", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String taskList(String taskId) {
        if (taskId == null) {
            return Constants.AjaxCode.PARAMLOSS;
        }
        TaskConfig config = new TaskConfig();
        config.setId(taskId);
        TaskRecord.deleteRecord(config, getConfigFilePath());
        return Constants.AjaxCode.SUCCESS;
    }

    /**
     * 启动或停止所有任务
     *
     * @param status
     * @return
     */
    @RequestMapping(value = "/startOrStopAll", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String startAllTask(Integer status) {
        if (status == null || !(status == 0 || status == 1)) {
            return Constants.AjaxCode.PARAMLOSS;
        }
        if(TaskRecord.stopOrStartAll(getConfigFilePath(), status) <= 0){
            return Constants.AjaxCode.ERROR_TASK;
        }
        return Constants.AjaxCode.SUCCESS;
    }

    /**
     * 启动或关闭任务
     *
     * @param status
     * @return
     */
    @RequestMapping(value = "/updateStatus", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String taskList(Integer status, String taskId) {
        if (taskId == null || status == null || !(status == 0 || status == 1)) {
            return Constants.AjaxCode.PARAMLOSS;
        }
        TaskRecord.updateStatus(status, taskId, getConfigFilePath());
        return Constants.AjaxCode.SUCCESS;
    }

    private boolean checkParams(TaskConfig taskConfig) {
        if (StringUtil.hasBlank(taskConfig.getCompanyName(), taskConfig.getDbName(), taskConfig.getIpAddress(),
                taskConfig.getPortNo(), taskConfig.getUserName(), taskConfig.getUserPwd())) {
            return false;
        }
        if (taskConfig.getDbType() <= 0) {
            return false;
        }
        return true;
    }
}
